<?php
/**
 * 配送方式管理
 * @author cjli
 *
 */
class ShippingAction extends AdminAction
{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	//配送方式列表
	public function shipping_list()
	{
		$shippingList = ShippingModel::getShippingList();
		$shipping_db = D('shipping');
		$shipping_db->field('shipping.*,si.title');
		$shipping_db->join('shipping_items AS si ON si.shipping_id = shipping.id');
		$shippingList = $shipping_db->where(array('si.language_id' => C('language_id')))->order('listorder DESC')->select();
		$this->assign('shippingList', $shippingList);
		$this->display('shipping_list');
	}
	
	//添加配送方式
	public function shipping_add()
	{
		if(isset($_POST['dosubmit'])){
			$lastId = ShippingModel::updateShipping($_POST);
			$this->assign('closeWin', 1);
			if($lastId){
				S('shipping_list', NULL);
				//增加后台操作记录
				$language_id = C('language_id') ? C('language_id') : 1;
				$log_url = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
				$log_name = '添加配送方式：'.$_POST['shipping'][$language_id]['title'];
				//ManageModel::addEnterwebsiteLog($log_url,$log_name);
			
				$this->success('配送方式添加成功！');
			}else{
				$this->error('配送方式添加失败！');
			}
		}else{
			$this->display('shipping_form');
		}
	}
	
	//编辑配送方式
	public function shipping_edit()
	{
		$shipping_id = intval($_GET['sid']);
		if(isset($_POST['dosubmit'])){
			ShippingModel::updateShipping($_POST, $shipping_id);
			$this->assign('closeWin', 1);
			S('shipping_list', NULL);
			
			$this->success('配送方式编辑成功！');
		}else{
			$shipping = ShippingModel::getShippingInfo($shipping_id);
			$shipping['items'] = ShippingModel::getShippingItems($shipping_id);
			$this->assign('shipping', $shipping);
			$this->display('shipping_form');
		}
	}
	
	//删除配送方式
	public function shipping_delete()
	{
		$shipping_id = intval($_GET['sid']);
		$shipping_db = D('shipping');
		$shipping_db->delete($shipping_id);
		$shipping_items_db = D('shipping_items');
		
		$shipping_items_db->where(array('shipping_id' => $shipping_id))->delete();
		S('shipping_list', NULL);
		$this->success('配送方式删除成功！', U('shipping/shipping_list'));
	}
	
	//货运公司列表
	public function express_list()
	{
	
	}
	
	//快递跟踪
	public function express()
	{
		$config = SiteModel::getShopConfigList();
		if(isset($_POST['dosubmit'])){
			unset($_POST['dosubmit']);
			SiteModel::updateShopConfig($_POST);
			$this->success('保存成功', U('shipping/express'));
		}else{
			$this->assign('config', $config);
			$this->display('express');
		}
	}
	
	//弹窗选择配送地区
	public function shipping_select_area_ajax($is_ajax=1)
	{
		$countryList = SiteModel::getCountryList();
		$this->assign('countryList', $countryList);
		echo $this->fetch('shipping_select_area');
	}
	
	//查验配送方式名称是否重复
	public function shipping_checkname_ajax()
	{
		$name = trim($_GET['name']);
		$sid = intval($_GET['sid']);
		$where['name'] = $name;
		if($sid){
			$where['id'] = array('neq', $sid);
		}
		$product_db = D('shipping');
		$count = $product_db->where($where)->count();
		if($count){
			exit('0');
		}
		exit('1');
	}
}